public class Solution2181 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode mergeNodes(ListNode head) {
        ListNode first=new ListNode(-1,null);
        ListNode cur=first;
        int last=0;
        head=head.next;
        while (head!=null){
            if (head.val==0){
                ListNode listNode=new ListNode(last,null);
                cur.next=listNode;
                cur=listNode;
                last=0;
            }else {
                last+= head.val;
            }
            head=head.next;
        }
        return first.next;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2181().mergeNodes(new ListNode(0,new ListNode(3,new ListNode(1,new ListNode(0,new ListNode(4,new ListNode(5,new ListNode(2,new ListNode(0))))))))).next.val);
    }
}
